<?php


namespace app\admin\model\user;

use app\admin\model\BaseModel;

use app\admin\model\product\LoanRecord;
/**
 * 管理员管理
 * Class Admin
 * @package app\admin\model
 */
class User extends BaseModel
{
    
    public static function systemPage($where)
    {
        $model = new self;
        $model = $model->alias("u")->leftJoin("wechat_user w","w.uid = u.uid");
        $model = $model->order("create_time desc");


        switch ($where["type"]) {
            case "1":
                $model = $model->where("u.cut",0);
                break;
            case "2":
                $model = $model->where("u.cut",1);
                break;
            case "3":
                $model = $model->where("u.authentication","<>","");
                break;
            default:
                break;
        }

        if ($where['name'] != '') $model = $model->where('u.realname|u.phone',"like","%$where[name]%");
        if ($where['start_time'] != '') $model = $model->where('u.create_time','>',strtotime($where['start_time']." 00:00:00"));
        if ($where['end_time'] != '') $model = $model->where('u.create_time','<', strtotime($where['start_time']." 23:59:59"));
        
        $model = $model->field('u.*,w.nickname,w.avatar');
        $count = self::counts($model);
        if ($where['page'] && $where['limit']) $model = $model->page((int)$where['page'],(int)$where['limit']);
        
        $data = $model->select()->each(function($item){
            if($item['birthday'] == 0){
                $item['birthday'] = "";
            }else{
                $item['birthday'] = date("Y-m-d H:i:s",$item['birthday']);
            }
            $item['product_count'] = LoanRecord::where("uid",$item["uid"])->count();
        });
        $data = $data ? $data->toArray() : [];
        return compact("data","count");
    }

    /**
     * 获取用户信息
     * Class Admin
     * @package app\admin\model
     */
    public static function getUserInformation($uid)
    {
        $model = new self;
        $userInfo = $model->where("uid",$uid)->find();

        if ($userInfo) {
            $userInfo = $userInfo->toArray();
        }else{
            $userInfo = false;
        }

        return $userInfo;
    }

}